package com.hnkywl.sts.dao.cggl;

import net.ruixin.orm.hibernate.ComplexHibernateTemplate;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-8-19
 * Time: 下午6:50
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class BgcgdSyncDao {
    private JdbcTemplate jdbcTemplate;


    public void bgcgdSyncNew(Object[] cgd,List<Object[]> cgdmx){
        //验证数据
        checkNewCgdData(cgd,cgdmx);
        //插入主表信息
        addBgcgd(cgd);
        //明细信息
        addBgcgdMx(cgdmx);
    }
    public void bgcgdSyncRk(Object[] dhxx,List<Object[]> dhmx,Object[] wjswz,List<Object[]> wjswzmx,Long cgdId){
        //验证数据
        checkRkDada(dhxx,dhmx,cgdId);
        checkWjswzData(wjswz,wjswzmx);
        //主信息表更新
        rkBgcgd(dhxx,cgdId);
        //明细表更新
        rkBgcgdmx(dhmx);
        //未结算物资插入
        insertWjswz(wjswz);
        //未结算物资明细插入
        insertWjswzmx(wjswzmx);
    }
    public void bgcgdSyncTz(Long cgdId){
        tzBgcgd(cgdId);
    }
    public void bgcgdSyncJs(Long cgdId){
        String sql = "select count(1) from wjswz where wlid = ?";
        Integer count = jdbcTemplate.queryForInt(sql,cgdId);
        if(count == 0){
            throw new RuntimeException("送货单结算：不存在送货单cgdId:"+cgdId);
        }
        String sql1 = "update wjswz set jsbz = 1 where wlid = ?";
        jdbcTemplate.update(sql1,cgdId);
    }
    public void bgcgdSyncDelJs(Long cgdId){
        String sql = "select count(1) from wjswz where wlid = ?";
        Integer count = jdbcTemplate.queryForInt(sql,cgdId);
        if(count == 0){
            throw new RuntimeException("送货单删除结算：不存在送货单cgdId:"+cgdId);
        }
        String sql1 = "update wjswz set jsbz = 0 where wlid = ?";
        jdbcTemplate.update(sql1,cgdId);
    }

    public void bgcgdSyncDel(Long cgdId,String mxIds){
        delBgcgd(cgdId+200000,mxIds);
    }

    public void addBgcgd(Object[] cgd) {
        String sql = "insert into shd(stbh,shdh,gysbh,shdwmc,djzt,Zdrq,Xsdrq,shrq,zdr,shy,ysy,nian,Yue,tzflag,bz,djlx,gsdm,wlid,dcclbz) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,cgd);
    }

    public void addBgcgdMx(List<Object[]> mxList) {
        String sql = "insert into shdmx(shdh,htbh,wzbh,wzmc,shsl,sssl,sumsssl,shdj,notaxdj,xhgg,th,cz,jldw,phsb,Wlid) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        if(!CollectionUtils.isEmpty(mxList)){
            for(Object[] obj : mxList){
               jdbcTemplate.update(sql,obj);
            }
        }

    }

    public void tzBgcgd(Long cgdId){
        //验证信息
        String sql1 = "select count(1) from shd where wlid = ?";
        Integer count = jdbcTemplate.queryForInt(sql1,cgdId);
        if(count == 0){
            throw new RuntimeException("不存在存在送货单cgdId:"+cgdId);
        }
        //更新送货单调整状态
        String sql = "update shd set tzflag = 1 where wlid = ?";
        jdbcTemplate.update(sql,cgdId);
        //更新未结算物资调整状态
        String sql2 = "update wjswz set tzflag = 1 where wlid = ?";
        jdbcTemplate.update(sql2,cgdId);
    }

    public void rkBgcgd(Object[] dhxx,Long cgdId){
        String sql = "update shd set djzt = ?, shy = ?, ysy = ?, shrq = ? where wlid = ?";
        jdbcTemplate.update(sql,dhxx[0],dhxx[1],dhxx[2],dhxx[3],cgdId);
    }

    public void rkBgcgdmx(List<Object[]> mx){
        String sql = "update shdmx set shsl = ?,sssl = ?,sumsssl = ? where wlid = ?";
        if(!CollectionUtils.isEmpty(mx)){
            for(Object[] obj : mx){
                jdbcTemplate.update(sql,obj[0],obj[1],obj[2],obj[3]);
            }
        }
    }

    public void delBgcgd(Long cgdId,String mxIds){
        //验证信息
        String sql2 = "select count(1) from shd where wlid = ?";
        Integer count = jdbcTemplate.queryForInt(sql2,cgdId);
        if(count == 0){
            throw new RuntimeException("不存在存在送货单cgdId:"+cgdId);
        }
        //执行删除动作
        String sql = "delete from shd where wlid = ?";
        jdbcTemplate.update(sql,cgdId);
        String sql1 = "delete from shdmx where wlid in ("+mxIds+")";
        jdbcTemplate.update(sql1);
    }

    public void insertWjswz(Object[] wjswz) {
        String sql = "insert into wjswz(stbh,shdwmc,ksbh,shdh,dhrq,gysbh,jsbz,tzflag,gsdm,lb,wlid) values (?,?,?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,wjswz);
    }
    public void insertWjswzmx(List<Object[]> wjswzmx) {
        String sql = "insert into wjswzmx(stbh,wzbh,htbh,sssl,shdj,notaxdj,wzmc,xhgg,th,cz,jldw,phsb,Wlid) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
        if(!CollectionUtils.isEmpty(wjswzmx)){
            for(Object[] obj : wjswzmx){
                jdbcTemplate.update(sql,obj);
            }
        }
    }
    private void checkNewCgdData(Object[] cgd,List<Object[]> mxList){
        if(cgd[17] == null){
            throw new RuntimeException("采购单Id为空");
        }
        if(cgd[0] == null){
            throw new RuntimeException("收条编号为空,cgdId:"+cgd[17]);
        }
        if(cgd[1] == null){
            throw new RuntimeException("送货单号为空,cgdId:"+cgd[17]);
        }
        if(cgd[2] == null){
            throw new RuntimeException("供应商编号为空,cgdId:"+cgd[17]);
        }
        if(cgd[3] == null){
            throw new RuntimeException("收货单位名称为空,cgdId:"+cgd[17]);
        }
        if(cgd[5] == null){
            throw new RuntimeException("制单日期为空,cgdId:"+cgd[17]);
        }
        if(cgd[6] == null){
            throw new RuntimeException("需送到日期为空,cgdId:"+cgd[17]);
        }
        if(cgd[8] == null){
            throw new RuntimeException("制单人为空,cgdId:"+cgd[17]);
        }
        String sql = "select count(1) from shd where wlid = ?";
        Integer count = jdbcTemplate.queryForInt(sql,cgd[17]);
        if(count != 0){
            throw new RuntimeException("已存在存在送货单,送货单号:"+cgd[0] + ",cgdId:"+cgd[17]);
        }
        if(!CollectionUtils.isEmpty(mxList)){
            for(Object[] obj : mxList){
                if(obj[0] == null){
                    throw new RuntimeException("明细送货单号为空,cgdId:"+cgd[17]);
                }
                if(obj[1] == null){
                    throw new RuntimeException("明细合同编号为空,cgdId:"+cgd[17]);
                }
                if(obj[2] == null){
                    throw new RuntimeException("明细物资编码为空,cgdId:"+cgd[17]);
                }
                if(obj[3] == null){
                    throw new RuntimeException("明细收货数量为空,cgdId:"+cgd[17]);
                }
                if(obj[7] == null){
                    throw new RuntimeException("明细含税单价为空,cgdId:"+cgd[17]);
                }
                if(obj[8] == null){
                    throw new RuntimeException("明细不含税单价为空,cgdId:"+cgd[17]);
                }
            }
        }
    }

    private void checkRkDada(Object[] dhxx,List<Object[]> dhmx,Long cgdId){
        String sql = "select count(1) from shd where wlid = ?";
        Integer count = jdbcTemplate.queryForInt(sql,cgdId);
        if(count == 0){
            throw new RuntimeException("不存在送货单,cgdId:"+cgdId);
        }
        if(dhxx[0] == null){
            throw new RuntimeException("单据状态为空,cgdId:"+cgdId);
        }
        if(dhxx[1] == null){
            throw new RuntimeException("收货员为空,cgdId:"+cgdId);
        }
        if(dhxx[2] == null){
            throw new RuntimeException("验收员为空,cgdId:"+cgdId);
        }
        if(dhxx[3] == null){
            throw new RuntimeException("收货日期为空,cgdId:"+cgdId);
        }
        if(!CollectionUtils.isEmpty(dhmx)){
            for(Object[] obj : dhmx){
                if(obj[0] == null){
                    throw new RuntimeException("收货数量为空,cgdId:"+cgdId);
                }
                if(obj[1] == null){
                    throw new RuntimeException("实收数量为空,cgdId:"+cgdId);
                }
                if(obj[2] == null){
                    throw new RuntimeException("总数量为空,cgdId:"+cgdId);
                }
            }
        }
    }

    private void checkWjswzData(Object[] wjswz,List<Object[]> wjswzmx){
        if(wjswz[10] == null) {
            throw new RuntimeException("采购单Id为空");
        }
        if(wjswz[0] == null){
            throw new RuntimeException("收条编号为空");
        }
        if(wjswz[1] == null){
            throw new RuntimeException("收货单位名称为空");
        }
        if(wjswz[2] == null){
            throw new RuntimeException("科室编号为空");
        }
        if(wjswz[3] == null){
            throw new RuntimeException("送货单号为空");
        }
        if(wjswz[4] == null){
            throw new RuntimeException("到货日期为空");
        }
        if(wjswz[5] == null){
            throw new RuntimeException("供应商编号为空");
        }
        String sql = "select count(1) from wjswz where wlid = ?";
        Integer count = jdbcTemplate.queryForInt(sql,wjswz[10]);
        if(count != 0){
            throw new RuntimeException("已存在送货单");
        }
        if(!CollectionUtils.isEmpty(wjswzmx)){
            for(Object[] obj : wjswzmx){
                if(obj[12] == null){
                    throw new RuntimeException("wlid为空");
                }
                if(obj[0] == null){
                    throw new RuntimeException("收条为空");
                }
                if(obj[1] == null){
                    throw new RuntimeException("物资编号为空");
                }
                if(obj[2] == null){
                    throw new RuntimeException("合同编号为空");
                }
                if(obj[3] == null){
                    throw new RuntimeException("实收数量为空");
                }
                if(obj[4] == null){
                    throw new RuntimeException("含税单价为空");
                }
                if(obj[5] == null){
                    throw new RuntimeException("不含税单价为空");
                }
            }
        }
    }
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
